﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FileDB;
namespace Rem.Objects.Forms
{
    public partial class CustomerForm : BaseForm
    {
        public Customer Customer { get { return this.Data as Customer; } }
        private void Remove(object sender, EventArgs e)
        {
            if (this.Customer.Id <= 0)
            {
                return;
            }

            DialogResult result = MessageBox.Show("确定删除此客户！", "客户删除", MessageBoxButtons.YesNo);
            if (result != DialogResult.Yes)
            {
                return;
            }
            try
            {
                Cursor.Current = Cursors.Arrow;
                if (this.Customer.Delete())
                {
                    if (base.Deleted != null)
                    {
                        base.Deleted(this, null);
                    }
                }
                else
                {
                    MessageBox.Show("删除出错或数据不存在！", "客户删除", MessageBoxButtons.OK);
                }
            }
            catch (InvalidOperationException invalidException)
            {
                //todo: log
                MessageBox.Show("删除出错！", "客户删除", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                //todo: log
                MessageBox.Show("已有数据关联到此客户！", "客户删除", MessageBoxButtons.OK);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
            this.Close();
        }

        public const string TextTemplate = "客户#{0}-{1}";
        public CustomerForm(Customer employee)
        {
            InitializeComponent();
            
            this.Data = employee;
            DisplayInfo();
            this.Saved += new EventHandler(AfterSaved);
        }

        public CustomerForm()
            : this( new Customer())
        {
            this.Text = "新建客户";
        }

        public override BaseObjectTypes BaseType
        {
            get
            {
                return BaseObjectTypes.Customer;
            }
        }

        public void AfterSaved(object sender, EventArgs e)
        {
            Customer employee = sender as Customer;
            this.Text = string.Format(TextTemplate, employee.Id, employee.Name);
        }

        private void Save(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.Arrow;
                this.Customer.Name = this.nameTextBox.Text;
                this.Customer.CardId = this.cardIdTextBox.Text;
                this.Customer.Phone = this.phonesTextBox.Text;
                this.Customer.Comment = this.commentTextBox.Text;

                Customer.Save();
                base.Save();
                DisplayInfo();
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }

        }

        private void DisplayInfo()
        {
            if (this.Customer != null)
            {
                this.lblID.Text = this.Customer.Id.ToString();
                this.nameTextBox.Text = this.Customer.Name ;
                this.cardIdTextBox.Text = this.Customer.CardId  ;
                this.phonesTextBox.Text =this.Customer.Phone ;
                this.commentTextBox.Text = this.Customer.Comment;
                this.Text = string.Format(TextTemplate, Customer.Id, Customer.Name);
            }
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            if (Customer.Id > 0)
                (new CustomerForm() { Dock = DockStyle.Fill}).Show();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            GoToForm.OpenForm("Id", "Name", BaseObjectTypes.Customer);
        }

        private void cloneToolStripButton_Click(object sender, EventArgs e)
        {
            if (this.Customer.Id > 0)
            {
                CustomerForm form = new CustomerForm((this.Customer.Clone()) as Customer);
                form.Show();
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            if (Customer.Id > 0)
            {
                if (this.Customer.SetAsSelfCompany())
                {
                    MessageBox.Show("设置成功!");
                }

            }
        }
    }
}
